<template>
	<view class="edit-my-ecard">
		<form @submit="formSubmit">
			<view class="form-item">
				<view class="label">姓名</view>
				<input class="value" name="nickname" :value="userInfo.nickname" />
			</view>
			<view class="form-item">
				<view class="label">电话</view>
				<input class="value" name="phone" :value="userInfo.phone" />
			</view>
			<view class="form-item">
				<view class="label">邮箱</view>
				<input class="value" name="email" :value="userInfo.email" />
			</view>
			<view class="form-item" style="border: none;">
				<button class="btn" form-type="submit" size="mini">保存</button>
			</view>
		</form>
	</view>
</template>

<script setup>
	import { storeToRefs } from 'pinia'
	import { useUserStore } from '../../store/user'

	// data
	const { info: userInfo } = storeToRefs(useUserStore()) // 用户信息

	// methods
	const formSubmit = (e) => {
		const { value } = e.detail
		if (!value.nickname || !value.phone) {
			uni.showToast({
				title: '姓名、电话不能为空',
				icon: 'error'
			})
			return
		}
		uni.showLoading({ title: '保存中……' })
		useUserStore().updataUserProfile({ id: userInfo.value.id, ...value }).then(data => {
			uni.navigateBack()
			uni.showToast({
				title: '保存成功',
				icon: 'success'
			})
		}).catch((err) => {
			uni.showToast({
				title: '保存失败',
				icon: 'error'
			})
		}).finally(() => {
			uni.hideLoading()
		})
	}
</script>

<style lang="scss" scoped>
	.btn {
		color: #ffffff;
		background-color: #1AAD19;
		border-color: #1AAD19
	}

	.edit-my-ecard {
		padding: 20rpx;
	}

	.form-item {
		display: flex;
		flex-direction: column;
		padding: 20rpx;
		border-bottom: 1px solid #eee;
		gap: 20rpx;

		&>.label {
			width: 80rpx;
			color: #bbb;
		}
	}
</style>